import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';

// 检查是否有网络
class ConnectionPage extends StatefulWidget {
  @override
  _ConnectionPageState createState() => _ConnectionPageState();
}

class _ConnectionPageState extends State<ConnectionPage> {
  bool connectionStatus = false;

  Future check() async {
    try {
      final result = await InternetAddress.lookup('jd.itying.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        connectionStatus = true;
        print("connected111 === $connectionStatus");
      }
    } on SocketException catch (_) {
      connectionStatus = false;
      print("not connected222 === $connectionStatus");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Internet Connection'),
      ),
      body: connectionStatus ?  Center(
        child: RaisedButton(
          child: Text('点击进入首页'),
          onPressed: () {
            Navigator.pushNamed(context, '/');
          }(),
        ),
      ) : SafeArea(
        child: Container(
          alignment: Alignment.center,
          child: GestureDetector(
            onTap: () {
              setState(() {
                check();
              });
            },
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(
                  Icons.wifi,
                  color: Colors.grey[200],
                  size: 80,
                ),
                Text('网络出问题了～请您查看网络设置'),
                Text('点击屏幕,重新加载'),
              ],
            ),
          ),
        ),
      ),

    );
  }
}